package com.jic.product.impl;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.product.entity.ProductGuaranteeRelation;
import com.jic.product.mapper.ProductGuaranteeRelationMapper;
import com.jic.product.service.ProductGuaranteeRelationService;
import com.jic.product.rest.response.ProductGuaranteeResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品保障关联表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-01-13 08:07:30
 */
@Service
public class ProductGuaranteeRelationServiceImpl implements ProductGuaranteeRelationService {

    @Autowired
    private ProductGuaranteeRelationMapper productGuaranteeRelationMapper;

    @Override
    public long insert(ProductGuaranteeRelation productGuaranteeRelation) {
        long flag = productGuaranteeRelationMapper.insert(productGuaranteeRelation);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return productGuaranteeRelationMapper.delete(id);
    }

    @Override
    public long update(ProductGuaranteeRelation productGuaranteeRelation) {
        return productGuaranteeRelationMapper.update(productGuaranteeRelation);
    }

    @Override
    public ProductGuaranteeRelation queryByPrimaryKey(Long id) {
        return productGuaranteeRelationMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<ProductGuaranteeRelation> query(ProductGuaranteeRelation productGuaranteeRelation) {
        return productGuaranteeRelationMapper.query(productGuaranteeRelation);
    }

    @Override
    public PageResult<ProductGuaranteeRelation> queryPage(ProductGuaranteeRelation productGuaranteeRelation, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<ProductGuaranteeRelation> list = productGuaranteeRelationMapper.queryPage(startIndex, pageQuery.getLimit(), productGuaranteeRelation);
        long count = productGuaranteeRelationMapper.queryPageCount(productGuaranteeRelation);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    //h5根据商品id查询商品保障
    @Override
    public List<ProductGuaranteeResponse> queryByProductId(long id) {
        return productGuaranteeRelationMapper.queryByProductId(id);
    }


    @Override
    public long[] guaranteeId(ProductGuaranteeRelation productGuaranteeRelation) {
        List<ProductGuaranteeRelation> productGuaranteeRelationList = productGuaranteeRelationMapper.query(productGuaranteeRelation);
        //重新封装商品保障id
        long[] guaranteeId = new long[productGuaranteeRelationList.size()];
        for (int i = 0; i < guaranteeId.length; i++) {
            guaranteeId[i] = productGuaranteeRelationList.get(i).getGuaranteeId();
        }
        return guaranteeId;
    }
}